
from suds.client import Client
import XMLHandler
import time


class WebserviceSuds():
    TEST = False
    def __init__(self, instance):
        self.signal = instance
    def webservice_suds(self, url, msg, operetions):
        c = Client(url)
        if operetions == 'GenerateMesInfo':
            result = c.service.GenerateMesInfo(msg)
        elif operetions == 'GeneratePQC':
            result = c.service.GeneratePQC(msg)
        print(result)
        return result


    def getDispatch(self,Number):
        msg = '<?xml version="1.0" encoding = "UTF-8"?>' \
               '<STD_IN>' \
               '<EDI_user>MESUSER</EDI_user>' \
               '<EDI_pwd > MESUPWD</EDI_pwd>' \
               '<ServiceName />' \
               '<OperationCode></OperationCode>' \
               '<ObjectID>ReadZYXX</ObjectID>' \
               '<Service>' \
               '<data>' \
               '<CPH>'+Number+'</CPH >' \
               '</data>' \
               '</Service >' \
               '</STD_IN>'
        result = ""
        if self.TEST:
            result = '<?xml version="1.0" encoding="utf-8"?><STD_OUT><ObjectID>ReadZYXX</ObjectID><Status>0</Status><Error></Error><OperDate>2020-06-04 10:47:22</OperDate><DATA><ZYDDH>Z000CA205190010</ZYDDH><SJTCSL>1980</SJTCSL><KSSJ>2020/5/19 0:00:00</KSSJ></DATA><DATA><ZYDDH>10012006010006</ZYDDH><SJTCSL>2</SJTCSL><KSSJ>2020/6/1 17:24:29</KSSJ></DATA></STD_OUT>'
        else:
            result = self.webservice_suds("http://10.18.255.36/MesService/ServICE.asmx?wsdl",msg,"GenerateMesInfo")

        self.signal.emit("setDispatch", XMLHandler.getXMLHandler(result).getDispatch())



    def sendMes(self,mes):
        date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        msg = '<?xml version="1.0" encoding = "UTF-8"?>' \
              '<STD_IN>' \
              '<ObjectID>ic_pqc_record</ObjectID>' \
              '<Service>' \
              '<PQC>' \
              '<JODNO>'+mes.disptch+'</JODNO>' \
              '<PROSN>'+mes.sn+'</PROSN> ' \
              '<PQCSTATION>'+mes.working+'</PQCSTATION>' \
              '<PQCWORKTEAM>'+mes.item+'</PQCWORKTEAM>' \
              '<PQCWORKWDER>'+mes.classes+'</PQCWORKWDER>' \
              '<PQCOPERATOR>'+mes.maker+'</PQCOPERATOR>' \
              '<PQCCOLLECT>0</PQCCOLLECT>' \
              '<PQCQUALSTATE>0</PQCQUALSTATE>' \
              '<PQCERRORCODE></PQCERRORCODE>' \
              '<PQCTESTDT>'+date+'</PQCTESTDT>' \
              '<PQCIP>'+mes.ip+'</PQCIP>' \
              '<PQCLOCTION></PQCLOCTION>' \
              '</PQC>' \
              '</Service>' \
              '</STD_IN>'
        print(msg)
        result = ""
        if self.TEST:
            result = '<STD_OUT><Status>0</Status><Message></Message></STD_OUT>'
        else:
            result = self.webservice_suds("http://10.18.255.36/MESService_TEST/PQC_Service.asmx?wsdl", msg, "GeneratePQC")
        try:
            xh = XMLHandler.getXMLHandler(result)
            if xh.isSuccess():
                self.signal.emit("showBottom","抛mes成功")
            else:
                self.signal.emit("mesResultError", False,xh.error)
        except Exception as e:
         print("read :异常", e)

    def check(self,ygdm):
        msg = '<?xml version="1.0" encoding = "UTF-8"?>' \
              '<STD_IN>' \
              '<EDI_user>MESUSER</EDI_user>' \
              '<EDI_pwd > MESUPWD</EDI_pwd>' \
              '<ServiceName />' \
              '<OperationCode></OperationCode>' \
              '<ObjectID>ReadYGXXByCode</ObjectID>' \
              '<Service>' \
              '<data>' \
              '<YGDM>'+ygdm+'</YGDM>'\
              '</data>' \
              '</Service >' \
              '</STD_IN>'
        result = ""
        if self.TEST:
            result = '<?xml version="1.0" encoding="utf-8"?><STD_OUT><ObjectID>ReadYGXXByCode</ObjectID><Status>0</Status><Error></Error><OperDate>2020-06-04 11:32:58</OperDate><DATA><YGDM>T54947</YGDM><YGMC>洪琰-T54947</YGMC></DATA></STD_OUT>'
        else:
            result =  self.webservice_suds("http://10.18.255.36/MesService/ServICE.asmx?wsdl", msg, "GenerateMesInfo")

        self.signal.emit("loginResult", XMLHandler.getXMLHandler(result).getYGDM())





